using System;
using Gtk;
using Mono.SANE;

public partial class MainWindow: Gtk.Window
{	
	private Scanner scanner;
	
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
		
		
		this.findScanners();
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	private void findScanners()
	{
		if (SANEControl.IsSANEAvailable)
		{
			if (SANEControl.Scanners.Count > 0)
			{
				foreach (Scanner s in SANEControl.Scanners.Values) {
					this.scanner = s;
				}
						
				this.lblScanner.Text =this.scanner.Model + " (" + this.scanner.Deviceid + ")";
			}
			else
				Console.WriteLine("No scanners found!");
		}
		else
			Console.WriteLine("Sane not installed!");
	}

	protected virtual void preview (object sender, System.EventArgs e)
	{
		// Scan a picture
		Gdk.Pixbuf picture;
		try
		{
			System.Drawing.Image img = this.scanner.ScanImage();
			img.Save("/home/christian/test.tiff", System.Drawing.Imaging.ImageFormat.Tiff);
			
		} 
		catch (NoDocumentLoadedException) 
		{
			Gtk.MessageDialog dlg = new Gtk.MessageDialog (this, DialogFlags.Modal,
				MessageType.Error, ButtonsType.Ok, "No pages were loaded in the scanner.");
			dlg.Run ();
			dlg.Destroy ();
		}
	}
}